USE [FootballPoll]
GO

/****** Object:  StoredProcedure [dbo].[GetMostRecentVoteByUserName]    Script Date: 12/02/2011 13:28:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetMostRecentVoteByUserName]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetMostRecentVoteByUserName]
GO

USE [FootballPoll]
GO

/****** Object:  StoredProcedure [dbo].[GetMostRecentVoteByUserName]    Script Date: 12/02/2011 13:28:31 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[GetMostRecentVoteByUserName] 
    /****** Pass in parameters  ******/
   
		@UserName VARCHAR(255) 
AS 
     /****** Explicit declaration for testing  ******/
    /*
		DECLARE @UserName VARCHAR(255) 
		SET @UserName = 'gonzo' 
	*/	
	/****** Explicit declaration for testing  ******/

SELECT v.userid, 
       v.DATE,
       u.userName       
FROM   votes v 
       LEFT JOIN users u 
         ON u.id = v.userid 
            AND u.username = @UserName 
WHERE  v.DATE = (SELECT MAX(DATE) 
                 FROM   votes AS v 
                 WHERE  v.userid = u.id) 
GROUP  BY v.userid, 
          v.DATE,
          u.userName 
          

GO

